﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SH.STD.Model;
using SH.STD.DataProvider;
using System.Data;

namespace SH.STD.Web.Admin
{
    public partial class DepartmentList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.SetTab(TabType.Admin);

            if (!Page.IsPostBack)
            {
                this.BindData(this.GetSql());
            }
        }

        private void BindData(string sql)
        {
            var dt = Cmn.DB.getDataTable(sql);

            this.rptDepartments.DataSource = dt;
            this.rptDepartments.DataBind();
            this.lblCnt1.Text = dt.Rows.Count.ToString();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindData(this.GetSql());
        }

        private string GetSql()
        {
            var ret =
@"SELECT *
  From Department
  WHERE Status=1 and ";
            var where = String.Empty;
            if (!String.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                where += "Name like '%" + this.txtName.Text.Trim() + "%' and ";
            }
            ret = where == String.Empty ? ret.Substring(0, ret.LastIndexOf(" and ")) : ret + " " + where.Substring(0, where.LastIndexOf(" and "));

            return ret;
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.txtName.Text = String.Empty;

            this.BindData(this.GetSql());
        }

        public static string ExportTable(DataTable dt)
        {
            var columnNames = new Dictionary<string, string>();
            columnNames.Add("部门名称", "Name");
            string ret = "<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">";
            ret += "<tr style=\"font-weight: bold; white-space: nowrap;\">";
            foreach (var col in columnNames)
            {
                ret += "<td>" + col.Key + "</td>";
            }
            ret += "</tr>";
            foreach (DataRow row in dt.Rows)
            {
                ret += "<tr>";
                foreach (var col in columnNames)
                {
                    ret += "<td>" + (row[col.Value].ToString() != "undefined" ? row[col.Value].ToString() : "") + "</td>";
                }
                ret += "</tr>";
            }
            ret += "</table>";

            return ret;
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            var dt = Cmn.DB.getDataTable(this.GetSql());
            ExportDsToXls(this.Page, HttpUtility.UrlEncode("部门管理", System.Text.Encoding.UTF8), dt);
        }

        public static void ExportDsToXls(Page page, string fileName, DataTable dt)
        {
            page.Response.Clear();
            page.Response.Buffer = true;
            page.Response.Charset = "GB2312";
            page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
            page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            page.Response.ContentType = "application/ms-excel";
            page.EnableViewState = false;
            page.Response.Write(ExportTable(dt));
            page.Response.End();
        }
    }
}